// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Text;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Net.Http.Headers;

namespace SnapObjects.Data.AspNetCore
{
    internal abstract class JsonOutputFormatter : TextOutputFormatter
    {
        public JsonOutputFormatter()
        {
            this.SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json"));

            this.SupportedEncodings.Add(Encoding.UTF8);
            this.SupportedEncodings.Add(Encoding.Unicode);
        }

        public override bool CanWriteResult(OutputFormatterCanWriteContext context)
        {
            if (base.CanWriteResult(context))
            {
                if (!String.IsNullOrWhiteSpace(context.ContentType.Value))
                {
                    return this.SupportedMediaTypes.Contains(context.ContentType.Value);
                }

                return true;
            }

            return false;
        }
    }
}
